package cn.nebula.music.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.nebula.music.domain.Comment;
import cn.nebula.music.service.CommentService;
import cn.nebula.music.service.SongService;
import cn.nebula.music.service.UserService;

@Controller
public class CommentController {

	@Autowired
	private CommentService commentService;

	@Autowired
	private SongService songService;

	@Autowired
	private UserService userService;

	@RequestMapping("/editComment")
	@ResponseBody
	public Map<String, Object> editComment(@RequestParam Integer id) throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("comment", commentService.findCommentById(id));
		values.put("songs", songService.findAllSongsOfBT());
		values.put("users", userService.findAllUsersOfBT());
		values.put("comments", commentService.findAllCommentsOfBT());
		return values;
	}

	@RequestMapping("/listAllComments")
	@ResponseBody
	public Map<String, Object> listAllComments() throws Exception {
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("comments", commentService.findAllCommentsOfBT());
		return values;
	}

	@RequestMapping("/newComment")
	@ResponseBody
	public Map<String, Object> newComment() throws Exception {
		Map<String, Object> values = new HashMap<>();
		values.put("songs", songService.findAllSongsOfBT());
		values.put("users", userService.findAllUsersOfBT());
		values.put("comments", commentService.findAllCommentsOfBT());
		return values;
	}

	@RequestMapping("/updateComment")
	@ResponseBody
	public boolean updateComment(@RequestBody Comment comment) throws Exception {
		commentService.updateComment(comment);
		return true;
	}

	@RequestMapping("/addComment")
	@ResponseBody
	public boolean addComment(@RequestBody Comment comment) throws Exception {
		commentService.addComment(comment);
		return true;
	}

	@RequestMapping("/deleteComment")
	@ResponseBody
	public boolean deleteComment(@RequestParam Integer id) throws Exception {
		commentService.deleteCommentById(id);
		return true;
	}
}
